package apis;

import java.util.HashMap;
import org.json.JSONObject;

import android.util.Log;
import cn.yiqijiao.AESUtil;
import cn.yiqijiao.Jiequ;
import utils.HttpRequestAndResponse;
import utils.UserInfo_Properties;

public class LoginApis extends UserInfo_Properties {
	static Jiequ jieQu = new Jiequ();
	static String value$, encryption;

	/**
	 * 发送验证码和验证验证码是否正确
	 * 
	 * @param phoneNumber
	 *            电话号码
	 * @param Verification_code
	 *            验证码
	 * @return JSONObject 对象
	 * @throws Exception
	 */
	public static JSONObject code_verification(String phoneNumber, String Verification_code) throws Exception {

		http$ = new HttpRequestAndResponse();
		final String LOGIN_1 = URL$ + "api/v4/verification_code/o";// 请求验证value
		final String LOGIN_2 = URL$ + "api/v4/verification_code";// 请求加密验证
		final String LOGIN_3 = URL$ + "api/v4/verification_code/" + phoneNumber;// 请求验证码登录验证

		/*
		 * 发送验证码 http://10.0.0.201:6060/api/v4/verification_code/o接口请求
		 */
		HashMap<String, String> map_1 = new HashMap<String, String>();
		map_1.put("phone", phoneNumber);
		map_1.put("code", "o93ksqoe//rwid2baiduHeEcod");
		String value = null;

		value = http$.doGet(LOGIN_1, map_1).getString("value");

		String valueEnd_six = jieQu.substring(value);// 截取value最后6位
		String aes_String = AESUtil.encrypt(valueEnd_six);// 编码
		// 拼接 /verification_code中的code字段的值
		String code = aes_String + "," + value;
		Log.d(TAG, "code=" + code);
		/*
		 * http://10.0.0.201:6060/api/v4/verification_code接口请求
		 */
		HashMap<String, String> map_2 = new HashMap<String, String>();
		map_2.put("phone", phoneNumber);
		map_2.put("code", code);
		map_2.put("validation_exist	", "false");
		map_2.put("data_code", "00");
		JSONObject jsonObject_2 = null;

		jsonObject_2 = http$.doPost(LOGIN_2, map_2);

		value$ = jsonObject_2.getString("value");
		encryption = jsonObject_2.getString("encryption");
		Log.d(TAG, "value$=" + value$);
		Log.d(TAG, "encryption=" + encryption);
		// 验证验证码是否正确
		// http://10.0.0.201:6060/api/v4/verification_code/18380339036接口请求
		HashMap<String, String> map_3 = new HashMap<String, String>();
		map_3.put("phone", phoneNumber);
		map_3.put("rand_chars", Verification_code);
		map_3.put("value", value$);
		map_3.put("encryption", encryption);

		JSONObject jsonObject_3 = null;
		jsonObject_3 = http$.doGet(LOGIN_3, map_3);

		return jsonObject_3;
	}

	/**
	 * 登录接口.调用该接口可以直接登录，其内部调用了验证验证码的接口<br>
	 * 
	 * @param phoneNumber
	 * @param Verification_code
	 * @return 返回一个JSONObject 对象，包含session_id和user_id<br>
	 * 返回的数据格式和字段名称：<br>
	 * {"birthday":0,<br>
	 * "session_id":"fdF3CmjULAVParjDlnbYynCoptoO5LrIRbriE3MkNg2gFA5EzrKHneVZir5onx8t",<br>
	 * "phone":"18300000000",<br>
	 * "teacher_subject":[],<br>
	 * "login_time":1469671834639,<br>
	 * "user_type":75,<br>
	 * "oid":"56c68888421aa95def000004"<br>
	 * ,"name":"天下无敌！",<br>
	 * "gender":0,<br>
	 * "user_status":1,<br>
	 * "portrait":""}<br>
	 * @throws Exception
	 *
	 */
	public static JSONObject login(String phoneNumber, String Verification_code) throws Exception {

		final String LOGIN_4 = URL$ + "api/v4/sessions/" + phoneNumber;// 请求验证码登录验证
		JSONObject jsonObject_4 = null;
		http$ = new HttpRequestAndResponse();
		JSONObject jsonObject = LoginApis.code_verification(phoneNumber, Verification_code);

		if (jsonObject.getString("message").equals("验证成功")) {
			HashMap<String, String> map_4 = new HashMap<String, String>();
			map_4.put("phone", phoneNumber);
			map_4.put("rand_chars", Verification_code);
			map_4.put("value", value$);
			map_4.put("encryption", encryption);
			jsonObject_4 = http$.doPost(LOGIN_4, map_4);

			Log.d(TAG, "jsonObject_4=" + jsonObject_4.toString());
		} else {
			Log.e(TAG, "验证验证码接口异常或验证码不正确");
		}
		return jsonObject_4;
	}
}
